package com.control.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.CustomerDetailInfoDAO;
import com.dao.CustomerInfoDAO;
import com.dao.OrderDetailDAO;
import com.dao.OrderGoodsInfoDAO;
import com.dao.OrderInfoDAO;
import com.dao.ServiceAssessmentDAO;
import com.entity.CustomerDetailInfo;
import com.entity.CustomerInfo;
import com.entity.GoodsInfo;
import com.entity.ServiceAssessment;
import com.util.PublicToolCheckParam;
import com.util.PublicToolParam;
import com.util.Validate;

@SuppressWarnings("serial")
public class OrderInfo extends HttpServlet {
	/**
	 * 提交挂号信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	public static void showConfirmSendInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = Validate.replaceHtml(request.getParameter("name"));
		String movePhone = Validate.replaceHtml(request.getParameter("movePhone"));
		if (!PublicToolCheckParam.checkNullAndEmpty(movePhone)) {
			movePhone = "无";
		}
		String address = Validate.replaceHtml(request.getParameter("address"));
		HttpSession session = request.getSession();
		ArrayList<GoodsInfo> shopList = (ArrayList<GoodsInfo>) session.getAttribute(PublicToolParam.SHOPLIST);
		CustomerInfoDAO cdao = new CustomerInfoDAO();
		CustomerInfo customerInfoList = cdao.getCustomerInfo(session.getAttribute("email").toString());
		int customerId = customerInfoList.getId();
		CustomerDetailInfoDAO cddao = new CustomerDetailInfoDAO();
		if (cddao.isCustomerDetailInfo(customerId) == true) {
			cddao.updateCustomerDdtailInfo(customerId, name,movePhone, address);
			CustomerDetailInfo customerDetailInfoList = cddao.getCustomerDetailInfo(customerId);
			request.setAttribute("customerDetailInfoList",customerDetailInfoList);
		} else {
			cddao.addCustomerDetailInfo(customerId, name, movePhone,address);
			CustomerDetailInfo customerDetailInfoList = cddao.getCustomerDetailInfo(customerId);
			request.setAttribute("customerDetailInfoList",customerDetailInfoList);
		}
		request.setAttribute(PublicToolParam.SHOPLIST, shopList);
		request.setAttribute("customerInfoList", customerInfoList);
		request.getRequestDispatcher("../filterWeb/confirmSendInfo.jsp").forward(request, response);
	}

	public static void showOrderInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String orderId = (String) request.getParameter("orderId");
		OrderGoodsInfoDAO ogdao = new OrderGoodsInfoDAO();
		ArrayList<Object> orderList = ogdao.getOrderGoodsInfo(Integer.parseInt(orderId));

		//  治疗信息列表
		OrderDetailDAO wgdao = new OrderDetailDAO();
		ArrayList<Object> orderWuliuInfoList = wgdao.getOrderWuliuInfo(Integer
				.parseInt(orderId));
		request.setAttribute("orderWuliuInfoList", orderWuliuInfoList);
		HttpSession session = request.getSession();

		CustomerInfoDAO cdao = new CustomerInfoDAO();
		CustomerInfo customerInfoList = cdao.getCustomerInfo(session
				.getAttribute("email").toString());
		int customerId = customerInfoList.getId();
		ServiceAssessmentDAO sdao = new ServiceAssessmentDAO();
		String isInsert = Validate
				.replaceHtml(request.getParameter("isInsert"));
		if (isInsert != null && isInsert.equals("insert")) {

			String serviceAssessment = Validate.replaceHtml(request
					.getParameter("serviceAssessment"));
			sdao.insertServiceAssessment(customerId, serviceAssessment,
					Integer.parseInt(orderId));
			ArrayList<Object> serviceAssessmentList = sdao
					.selectServiceAssessment(Integer.parseInt(orderId));
			if (serviceAssessmentList != null) {
				for (int i = 0; i < serviceAssessmentList.size(); i++) {
					ServiceAssessment serviceAssessmentInfo = (ServiceAssessment) serviceAssessmentList
							.get(i);
					out.println("" + serviceAssessmentInfo.getAssessmentTime()
							+ "&nbsp;&nbsp;&nbsp;&nbsp;您留言评价道：&nbsp;&nbsp;"
							+ serviceAssessmentInfo.getServiceAssessment()
							+ "<br/><br/>");
				}
			}

		} else {
			ArrayList<Object> serviceAssessmentList = sdao
					.selectServiceAssessment(Integer.parseInt(orderId));

			request.setAttribute("serviceAssessmentList", serviceAssessmentList);
			request.setAttribute("orderList", orderList);

			request.setAttribute("orderId", orderId);

			request.getRequestDispatcher("../filterWeb/orderInfo.jsp").forward(
					request, response);
		}
	}

	public static void th(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String orderId = Validate.replaceHtml(request.getParameter("orderId"));
		OrderInfoDAO odao = new OrderInfoDAO();
		odao.updateOrderStatus(Integer.parseInt(orderId), 101);
		OrderGoodsInfoDAO ogdao = new OrderGoodsInfoDAO();
		ogdao.updateOrderStatus(Integer.parseInt(orderId), 101);

		response.sendRedirect("../servlet/DoMethod?method=showMyInformation");

	}

	public static void sh(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String orderId = Validate.replaceHtml(request.getParameter("orderId"));
		OrderInfoDAO odao = new OrderInfoDAO();
		odao.updateOrderStatus(Integer.parseInt(orderId), 100);
		OrderGoodsInfoDAO ogdao = new OrderGoodsInfoDAO();
		ogdao.updateOrderStatus(Integer.parseInt(orderId), 100);

		response.sendRedirect("../servlet/DoMethod?method=showMyInformation");

	}

	/**
	 * Constructor of the object.
	 */
	public OrderInfo() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = (String) request.getParameter("method");
		if (method.equals("showConfirmSendInfo")) {
			showConfirmSendInfo(request, response);
		} else if (method.equals("showOrderInfo")) {
			showOrderInfo(request, response);
		} else if (method.equals("sh")) {
			sh(request, response);
		} else if (method.equals("th")) {
			th(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
